<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="220px">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="userInfo.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="userInfo.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="userInfo.mobile"
                  :disabled="!!$route.params.id"
                  size="mini"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <!--重点：给组件一个value属性，一个input事件，value属性用于传值，input事件用于修改值-->
                <SelectTree v-model="userInfo.departmentId" msg="msg" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="userInfo.formOfEmployment" size="mini" class="inputW">
                  <el-option :value="1" label="正式" />
                  <el-option :value="2" label="非正式" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="userInfo.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="userInfo.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <UploadImage v-model="userInfo.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="submit">保存更新</el-button>
              <el-button size="mini" type="primary" @click="$router.push('/employee/detail/1')">跳转到管理员信息</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>

import SelectTree from '@/views/employee/components/select-tree.vue'
import { addEmpApi, getUserInfoDetailApi, updateUserInfoApi } from '@/api/employees'
import UploadImage from '@/views/employee/components/image-upload.vue'

export default {
  components: { UploadImage, SelectTree },
  data() {
    console.log(this)
    return {
      userInfo: {
        username: '', // 用户名
        mobile: '', // 手机号
        workNumber: '', // 工号
        formOfEmployment: null, // 聘用形式
        departmentId: null, // 部门id
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          {
            required: true,
            message: '用户名必填'
          },
          {
            min: 2,
            max: 6,
            message: '用户名必须2-6位'
          }
        ],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }, {
          //   pattern 正则表达式
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '请选择聘用形式', trigger: 'blur' }],
        departmentId: [{ required: true, message: '请选择部门', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' }],
        correctionTime: [ // 转正时间的校验
          {
            required: true,
            message: '转正时间必填',
            trigger: 'blur'
          },
          {
            validator: (rules, value, callback) => {
            //   1、判断一下用户是否已经选择了入职时间
            //   2、如果选择了入职时间，我们需要判断一下他是不是大于转正时间
            //   3、如果大于转正时间，需要校验失败
              if (this.userInfo.timeOfEntry) {
                if (new Date(this.userInfo.timeOfEntry) > new Date(value)) {
                  callback(new Error('入职时间不能大于转正时间'))
                  return
                }
              }
              // 如果上述两个条件都没走进去，校验通过
              callback()
            }
          }
        ]
      }
    }
  },
  watch: {
    '$route.params.id'() {
      console.log('2323')
    }
  },
  created() {
    // 直接去调用，新增员工的场景需要调用接口吗？
    // 是不是新增员工，不是新增员工，再去调用获取详情信息的接口
    // 编辑 >> 调用接口
    console.log('232332')
    if (this.$route.params.id) {
      this.getUserInfoDetail(this.$route.params.id)
    }
  },
  beforeRouteUpdate(to, from, next) {
    console.log('beforeRouteUpdate')
    // console.log(to)
    this.getUserInfoDetail(to.params.id)
    next()
  },
  methods: {
    async submit() {
      // 表单校验
      await this.$refs.userForm.validate()
      if (this.$route.params.id) {
        await updateUserInfoApi(this.userInfo)
        // 成功的提示
        this.$message.success('更新信息成功')
      } else {
        // 提交接口
        await addEmpApi(this.userInfo)
        // 成功的提示
        this.$message.success('添加成功')
      }

      this.$router.push('/employee')
    },
    // 获取详情数据接口
    async getUserInfoDetail(id) {
      this.userInfo = await getUserInfoDetailApi(id)
    }
  }
}
</script>

<style scoped lang="scss">
.edit-form {
  background: #fff;
  padding: 20px;
  .inputW {
    width: 380px
  }
}

</style>
